﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using Common;

public partial class website_admin_bbs_topic_replies : PageBase
{
    public string BBSTitle = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "delete":
                Delete();
                break;
            case "savereply":
                SaveReplay();
                break;
            case "getrepliescount":
                GetRepliesCount();
                break;
            case "loadingreplies":
                RepliesItems();
                break;
        }

        if (!IsPostBack)
        {
            if (Utils.IsNumber(RequestBase.GetQueryString("b_id")))
            {
                Model.BBS.BBSInfo _obj = (new BLL.BBS.BBSBO()).GetBBSInfo(int.Parse(RequestBase.GetQueryString("b_id")));
                BBSTitle = _obj.Title;
                lblTitle.InnerText = _obj.Title;
                lblAuthor.Text = _obj.UserNickName;
                lblPubDate.Text = _obj.LastPostTime.ToString();
                lblContent.Text = _obj.Content;
                ///(new BLL.BBS.BBSBO()).SetClickTotal(RequestBase.GetQueryInt("b_id", 0));
            }

            txtAuthor.Value = "客服回复";
        }
    }
 
    #region 保存评论
    Model.BBS.BBSReplyInfo BBSObj
    {
        get
        {
            Model.BBS.BBSReplyInfo _obj = new Model.BBS.BBSReplyInfo();
            _obj.BBSID = int.Parse(RequestBase.GetFormString("b_id"));
            _obj.Content = RequestBase.GetFormString("comments");
            _obj.UserNickName = RequestBase.GetFormString("author");
            _obj.PublishTime = DateTime.Now; 

            return _obj;
        }
    }

    void SaveReplay()
    {
        string result = "";

        //if (Validate("q_a_admin", 4))
        //{
            bool bl = (new BLL.BBS.BBSBO()).SaveBBSReply(BBSObj);

            if (bl)
                result = "succeeded";
            else
                result = "error.";
        //} 
        //else
        //    result = "当前用户无操作权限！"; 

        BLL.AjaxHelper.ResponseWrite(result);
    }
    #endregion

    void GetRepliesCount()
    {
        object _obj = BLL.Search.CommonSearch.ExecScalar("BBSReply", "COUNT(1)",
         new string[] { "BBSID" },
         new string[] { "Equal" },
         new string[] { "" + RequestBase.GetFormString("b_id") + "" },
         null,
         null);

        if (_obj != null)
        {
            BLL.AjaxHelper.ResponseWrite(_obj.ToString() + " 条回复");
        }
        else
        {
            BLL.AjaxHelper.ResponseWrite("0 条回复");
        }
    }

    void Delete()
    {  
        string res = "";

        //if (Validate("q_a_admin", 3))
        //{
            bool bl = (new BLL.BBS.BBSBO()).DeleteBBSReplyByID(int.Parse(RequestBase.GetFormString("r_id")), Model.BBS.BBSEnum.IDState.ID);

            if (bl)
            {
                (new BLL.BBS.BBSBO()).SetReplyTotal(int.Parse(RequestBase.GetFormString("b_id")));
                res = "succeeded"; 
            }
            else
                res = "error.";  
        //}
        //else
        //    res = "当前用户无操作权限！";


        BLL.AjaxHelper.ResponseWrite(res);
    }

    void RepliesItems()
    {
        try
        {
            string ctrl = "~/website_admin/bbs/_controls/ctlTopicList.ascx";
            Control uc_rep = LoadControl(ctrl);
            Repeater rptRepliesList = (Repeater)uc_rep.FindControl("rptRepliesList");

            DataTable dat = BLL.Search.CommonSearch.ExecSelect("BBSReply",
                     new string[] { "BBSID" },
                     new string[] { "Equal" },
                     new string[] { "" + RequestBase.GetFormString("b_id") + "" },
                     null,
                     null);

            rptRepliesList.DataSource = dat;
            rptRepliesList.DataBind();

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            rptRepliesList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
}
